package com.xingkeduo.remote;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.dto.CraftsmanDTO;
import com.xingkeduo.remote.feign.AdminFeignService;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chi  2019-03-13 10:43
 **/
@Service
@Slf4j
public class AdminRemoteService {

    @Resource
    private AdminFeignService adminFeignService;

    public String getAdminImg(String id) {
        CraftsmanDTO admin = getAdmin(id);
        if(admin!=null){
            return admin.getPath();
        }
        return null;
    }


    public CraftsmanDTO getAdmin(String id) {
        log.info("getAdmin:{}", id);
        JSONObject object = new JSONObject();
        object.put("id", id);
        ResponseUtils<CraftsmanDTO> craftsman = adminFeignService.getCraftsman(object.toString());
        log.info("getAdmin response:{}", craftsman);
        if(craftsman.success()){
            return craftsman.getData();
        }
        return null;
    }


    /**
     * 根据发型师id获取发型师名称
     * @param id
     * @return
     */
    @Cacheable(value = "cache:comments:getAdminName", key = "#id")
    public String getAdminName(String id) {
        CraftsmanDTO admin = getAdmin(id);
        if(admin!=null){
            return admin.getNickName();
        }
        return null;
    }


    /**
     * 根据用户id获取用户 昵称和头像
     * @param userId
     * @return
     */
    public Map<String,String> queryUserInfo(String userId){
        String userInfo = adminFeignService.getUserInfo(userId);
        JSONObject jsonObject = JSON.parseObject(userInfo);
        Map<String,String> map = (Map<String, String>) jsonObject.get("data");
        return map;
    }


    /**
     * 获取用户昵称
     * @param userId
     * @return
     */
    public String userNickName(String userId){
        Map<String, String> map = queryUserInfo(userId);
        return map.get("f_name");
    }


    /**
     * 获取用户ids
     * @param userId
     * @return
     */
    public String[]  queryUserIds(String userId) {
        List<Object>  list=new ArrayList<>();
        String userListStr = adminFeignService.queryUserListByUserId("{\"id\":" + userId + "}");
        log.info("queryUserIds userids:{}",  JSON.toJSONString(userListStr));
        if (StringUtils.isNotBlank(userListStr)) {
            JSONObject message = JSON.parseObject(userListStr);
            if ("1".equals(message.getOrDefault("status", "0").toString())) {
                list = message.getObject("data", List.class);
            }
        }
        if(list.isEmpty()){
            list.add(Long.valueOf(userId));
        }
        log.info("list:{}", userId);
        String[] arrays = list.stream().map(s->s+"").toArray(String[]::new);
        return arrays;
    }
}
